package com.qdaily.ui.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MotionEventCompat;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.qdaily.supportlibrary.util.LocalDisplay;
import com.qdaily.supportlibrary.views.banner.BannerAdapter;
import com.qdaily.supportlibrary.views.banner.SliderBanner;
import com.qdaily.ui.MainActivity;
import com.qdaily.ui.QDBaseActivity;
import com.qdaily.ui.R;
import com.qdaily.ui.data.QDConfigConstants;

/**
 * 闪屏页，新手指引。
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 5/14/15
 * Time: 17:06
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class WhatsNewActivity extends QDBaseActivity {
    private WhatsNewAdapter whatsNewAdapter;
    private com.qdaily.supportlibrary.views.banner.SliderBanner sliderBanner;

    private float mStartDragX;
    private OnSwipeOutListener mListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_whatsnew);

        this.sliderBanner = (SliderBanner) findViewById(R.id.sliderBanner);
        this.whatsNewAdapter = new WhatsNewAdapter();

        this.sliderBanner.setDotNum(whatsNewAdapter.getCount());
        this.sliderBanner.setAdapter(whatsNewAdapter);

        this.sliderBanner.setViewPagerOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (sliderBanner.getCurrentItem() == whatsNewAdapter.getCount() - 1) {
                    final int action = event.getAction();
                    float x = event.getX();
                    switch (action & MotionEventCompat.ACTION_MASK) {
                        case MotionEvent.ACTION_DOWN:
                            mStartDragX = x;
                            break;
                        case MotionEvent.ACTION_MOVE:
                            break;
                        case MotionEvent.ACTION_UP:
                            if (x < mStartDragX) {
                                mListener.onSwipeOutAtEnd();
                            } else {
                                mStartDragX = 0;
                            }
                            break;
                    }
                } else {
                    mStartDragX = 0;
                }
                return true;
            }
        });

        this.setOnSwipeOutListener(new OnSwipeOutListener() {
            @Override
            public void onSwipeOutAtEnd() {
                iWantGoHome();
            }
        });
    }

    private void setOnSwipeOutListener(OnSwipeOutListener listener) {
        mListener = listener;
    }

    private interface OnSwipeOutListener {
        void onSwipeOutAtEnd();
    }

    private class WhatsNewAdapter extends BannerAdapter {
        private int maxCount = 4;

        public WhatsNewAdapter() {
        }

        @Override
        public int getCount() {
            return maxCount;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public View getView(LayoutInflater layoutInflater, int position) {
            View currentView = layoutInflater.inflate(R.layout.view_whatsnew_item, null);
            ImageView imageViewWhatsNewTop = (ImageView) currentView.findViewById(R.id.imageViewWhatsNewTop);
            ImageView imageViewWhatsNewSlogan = (ImageView) currentView.findViewById(R.id.imageViewWhatsNewSlogan);
            TextView textViewWhatsNewEnter = (TextView) currentView.findViewById(R.id.textViewWhatsNewEnter);

            if (position == 0) {
                imageViewWhatsNewTop.setImageResource(R.drawable.icon_whats_new_slogan_1);
                imageViewWhatsNewTop.setLayoutParams(LocalDisplay.getLayoutParamsTopMargin(imageViewWhatsNewTop, 0.109375));

                imageViewWhatsNewSlogan.setImageResource(R.drawable.icon_whats_new_text_1);
                imageViewWhatsNewSlogan.setLayoutParams(LocalDisplay.getLayoutParamsBottomMargin(imageViewWhatsNewSlogan, 0.182291666666667));

                currentView.setBackgroundResource(R.drawable.icon_whats_new_bg_1);
            } else if (position == 1) {
                imageViewWhatsNewTop.setImageResource(R.drawable.icon_whats_new_slogan_2);
                imageViewWhatsNewTop.setLayoutParams(LocalDisplay.getLayoutParamsTopMargin(imageViewWhatsNewTop, 0.109375));

                imageViewWhatsNewSlogan.setImageResource(R.drawable.icon_whats_new_text_2);
                imageViewWhatsNewSlogan.setLayoutParams(LocalDisplay.getLayoutParamsBottomMargin(imageViewWhatsNewSlogan, 0.182291666666667));

                currentView.setBackgroundResource(R.drawable.icon_whats_new_bg_2);
            } else if (position == 2) {
                imageViewWhatsNewTop.setImageResource(R.drawable.icon_whats_new_slogan_3);
                imageViewWhatsNewTop.setLayoutParams(LocalDisplay.getLayoutParamsTopMargin(imageViewWhatsNewTop, 0.069270833333333));

                imageViewWhatsNewSlogan.setImageResource(R.drawable.icon_whats_new_text_3);
                imageViewWhatsNewSlogan.setLayoutParams(LocalDisplay.getLayoutParamsBottomMargin(imageViewWhatsNewSlogan, 0.182291666666667));

                currentView.setBackgroundResource(R.drawable.icon_whats_new_bg_3);
            } else if (position == 3) {
                imageViewWhatsNewTop.setLayoutParams(LocalDisplay.getLayoutParamsTopMargin(imageViewWhatsNewTop, 0.109375));
                imageViewWhatsNewTop.setImageResource(R.drawable.icon_whats_new_slogan_4);
                currentView.setBackgroundResource(R.drawable.icon_whats_new_bg_4);

                textViewWhatsNewEnter.setLayoutParams(LocalDisplay.getLayoutParamsBottomMargin(textViewWhatsNewEnter, 0.182291666666667));
                textViewWhatsNewEnter.setVisibility(View.VISIBLE);
                imageViewWhatsNewSlogan.setVisibility(View.GONE);
                textViewWhatsNewEnter.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iWantGoHome();
                    }
                });
            }
            return currentView;
        }

        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public int getPositionForIndicator(int position) {
            return position % getCount();
        }
    }

    private void iWantGoHome() {
        getQDConfigManager().saveBoolean(QDConfigConstants.KEY_IF_FIRST_ENTER, false);
        startActivity(new Intent(WhatsNewActivity.this, MainActivity.class));
        WhatsNewActivity.this.finish();
    }
}
